<template>
	<!-- <div class="panel-box"> -->
	<div class="left-box" ref="spAppCustom"></div>
	<!-- <div class="right-box">
			<choose-list></choose-list>
		</div>
	</div> -->
</template>

<script>
import chooseList from '@/components/chooseList';
import { CheckUtils } from '@/utils/checkUtils';

import { loadScript, loadStyle } from '@/utils/loadScript.js';

import { mapActions, mapState } from 'vuex';
import { ACTION_ADD_ENTITY, ACTION_REMOVE_ENTITY } from '@/store/actionTypes';

export default {
	name: 'customBusinessTab',
	components: {
		chooseList
	},
	props: {
		tabMainInfo: { type: Object, default: () => { } }
	},
	data() {
		return {
			businessDom: ''
		};
	},
	computed: {
		...mapState('selectData', ['selectedList', 'selectedUUIDMap'])
	},
	watch: {
		selectedList(val) {
			/** 右侧已选区更新时回传最新右侧已选内容 */
			let { name } = this.tabMainInfo;
			window.selectPepleAppTab[name].getUpdateSelected(val);
		}
	},
	methods: {
		...mapActions('selectData',[ACTION_ADD_ENTITY,ACTION_REMOVE_ENTITY]),
		/** 检查范围方法 */
		async onCheckScopeStatus(nodes) {
			let entity = nodes[0];
			let notIncludeSubDept = [];
			let checkContext = {
				currentPanel: '',
				notIncludeSubDept
			};
			let result = await CheckUtils.doCheckEntity(entity, checkContext);
			if (!result) {
				return;
			}
			this.selectDataToRight(nodes);
		},
		/** 增加选项
		 * @param { Array } list 选择添加的节点
		 */
		onAdd(list) {
			this[ACTION_ADD_ENTITY]({
				entities: list
			});
		},
		/** 删除选项
		 * @param { Array } list 选择要删除的节点
		 */
		onRemove(list) {
			this[ACTION_REMOVE_ENTITY]({
				entities: list
			});
		}
	},
	created() {
		/** 自定义选人页面中页签注册器
		 * @param { String } key 自定义页签的唯一性标识
		 * @param  { Object } options 注册器暴露给业务的选人核心方法
		 */
		window.registeryTab = function (key, options) {
			console.log('options', options);
			window.selectPepleAppTab = window.selectPepleAppTab || {};
			window.selectPepleAppTab[key] = options;
		};
	},
	mounted() {
		let { name, url } = this.tabMainInfo;
		/** 加载业务自定义选人css代码 */
		loadStyle(url, name);
		/** 加载业务自定义选人js代码 */
		loadScript(url, name).then(() => {
			window.selectPepleAppTab[name].init({
				el: this.$refs.spAppCustom,
				getSelectedtMap: JSON.parse(JSON.stringify(this.selectedList)),
				getSelectedData: JSON.parse(JSON.stringify(this.selectedUUIDMap)),
				onAdd: this.onAdd.bind(this),
				onRemove: this.onRemove.bind(this)
			});
		});
	}
};
</script>
